﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.WorkTaskGrade.BLL;
using Yipusoft.WorkTaskGrade.Model;
using Yipusoft.Evaluate.Model;
using Yipusoft.Evaluate.BLL;

public partial class Task_WorkTask_WholeView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindYearList();
            BindStartMonthList();
            BindEndMonthList();
            YearList.SelectedValue = DateTime.Now.Year.ToString();
            DateTime DateNow = DateTime.Now;
            YearList.SelectedValue = DateNow.Year.ToString();
            StartMonthList.SelectedValue = DateNow.Month.ToString();
            EndMonthList.SelectedValue = DateNow.Month.ToString();
            

            if (Request["year"] != null)
            {

                YearList.SelectedValue = Request["year"];
                StartMonthList.SelectedValue = Request["sm"];
                EndMonthList.SelectedValue = Request["em"];
            }
            CreateTable();
        }
    }

    protected void PopupPanel1_ValueChange(string Value, string RelationValue)
    {

    }

    private void BindYearList()
    {
        for (int i = 2008; i <= 2050; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "年";
            li.Value = i.ToString();
            YearList.Items.Add(li);
        }
    }
    private void BindStartMonthList()
    {
        for (int i = 1; i <= 12; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "月";
            li.Value = i.ToString();
            StartMonthList.Items.Add(li);
        }

    }
    private void BindEndMonthList()
    {
        for (int i = 1; i <= 12; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "月";
            li.Value = i.ToString();
            EndMonthList.Items.Add(li);
        }
    }
    private void CreateTable()
    {
        tb.Rows.Clear();
        //部门
        List<DepartmentInfo> Departments = Department.GetList(" Left(departmentid,6)='" + Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID.Substring(0, 6) + "' ");
        decimal[] total = new decimal[Departments.Count];
        decimal[] all = new decimal[Departments.Count];
        for (int i = 0; i < Departments.Count; i++)
        {
            total[i] = 0;
            all[i] = 0;
        }

        TableRow dr = new TableRow();

        dr.HorizontalAlign = HorizontalAlign.Right;
        TableCell cell = new TableCell();

        //头
        cell.Text = "月份";
        cell.Width = Unit.Pixel(30);
        cell.CssClass = "Td";
        dr.Cells.Add(cell);
        cell = new TableCell();
        cell.Width = Unit.Pixel(80);
        cell.Wrap = true;
        cell.CssClass = "Td";
        cell.Text = "处室";
        dr.Cells.Add(cell);
        cell = new TableCell();

        cell.CssClass = "Td";
        cell.ColumnSpan = 2;
        cell.Text = "工作任务";
        dr.Cells.Add(cell);
        //完成情况
        cell = new TableCell();
        cell.CssClass = "Td";
        cell.Text = "简要完成情况";

        dr.Cells.Add(cell);

        cell = new TableCell();
        cell.CssClass = "Td";
        cell.Text = "分值";
        cell.Width = Unit.Pixel(30);
        dr.Cells.Add(cell);

        cell = new TableCell();
        cell.Width = Unit.Pixel(30);
        cell.CssClass = "Td";
        cell.Text = "自评";
        dr.Cells.Add(cell);

        cell = new TableCell();
        cell.Width = Unit.Pixel(30);
        cell.CssClass = "Td";
        cell.Text = "核准";
        dr.Cells.Add(cell);
        tb.Rows.Add(dr);

        cell = new TableCell();
        cell.Width = Unit.Pixel(30);
        cell.CssClass = "Td";
        cell.Text = "终审";
        dr.Cells.Add(cell);
        tb.Rows.Add(dr);
        //加分
        cell = new TableCell();
        cell.CssClass = "Td";
        cell.Text = "加分";
        cell.Width = Unit.Pixel(30);
        dr.Cells.Add(cell);
        //月份
        TableCell MonthCell;

        for (int i = Convert.ToInt32(StartMonthList.SelectedValue); i <= Convert.ToInt32(EndMonthList.SelectedValue); i++)
        {
            int MonthCount = 0;
            MonthCell = new TableCell();

            MonthCell.CssClass = "Td";
            MonthCell.Text = i.ToString();
            TableCell DepartmentCell;


            //所有处室
            for (int j = 0; j < Departments.Count; j++)
            {
                int departentCount = 0;
                DepartmentCell = new TableCell();
                DepartmentCell.CssClass = "Td";
                DepartmentCell.Text = Departments[j].ShortName.Substring(5, Departments[j].ShortName.Length - 5);
                DepartmentCell.Width = Unit.Pixel(100);
                DepartmentCell.Wrap = false;
                //处室任务
                WorkTaskGrade Task = new WorkTaskGrade();
                List<WorkTaskGradeInfo> Infos = Task.GetList("  (IsYear = 0) AND (YEAR(TaskDate) =" + YearList.SelectedValue + ") AND (MONTH(TaskDate) =" + i + ") And Departmentid='" + Departments[j].DepartmentID + "' ");
                for (int n = 0; n < Infos.Count; n++)
                {
                    int ItemCount = 0;
                    TableRow row = new TableRow();
                    row.HorizontalAlign = HorizontalAlign.Right;
                    if (MonthCount == 0)
                    {
                        row.Cells.Add(MonthCell);
                    }
                    if (departentCount == 0)
                    {
                        row.Cells.Add(DepartmentCell);
                    }
                    MonthCount++;
                    departentCount++;
                    ItemCount++;
                    //任务
                    TableCell cell1 = new TableCell();

                    cell1.CssClass = "Td";

                    cell1.Wrap = true;
                    cell1.RowSpan = 1;
                    cell1.Text = "<a href=WorkTaskView.aspx?id=" + Infos[n].Id + " >" + Infos[n].WorkTask + "</a>";
                    row.Cells.Add(cell1);
                    List<WorkTaskItemInfo> ItemList = WorkTaskItem.GetList(" taskId='" + Infos[n].Id + "' order by OrderNo,CreateDate ");

                    if (ItemList.Count > 0)
                    {
                        WorkTaskItemInfo item = ItemList[0];
                        cell = new TableCell();
                        cell.Text = ItemCount + "、" + item.ItemContent;
                        cell.CssClass = "Td";
                        cell.Width = 150;
                        row.Cells.Add(cell);

                        cell = new TableCell();
                        cell.Text = item.Circs;
                        cell.CssClass = "Td";


                        row.Cells.Add(cell);

                        tb.Rows.Add(row);


                    }
                    else
                    {
                        cell1.ColumnSpan = 2;

                        //简单情况
                        TableCell cell6 = new TableCell();

                        cell6.CssClass = "Td";
                        cell6.Text = Infos[n].SimpleCircs;

                        row.Cells.Add(cell6);

                    }



                    //总分
                    TableCell cell2 = new TableCell();

                    cell2.CssClass = "Td";
                    cell2.Text = Infos[n].Marking.ToString();

                    row.Cells.Add(cell2);

                    //自评
                    TableCell cell3 = new TableCell();
                    cell3.CssClass = "Td";
                    if (Infos[n].SelfScore != -1)
                    {

                        cell3.Text = Infos[n].SelfScore.ToString();
                    }
                    else
                    {
                        cell3.Text = "未评分";
                    }
                    cell3.Wrap = false;

                    all[j] = all[j] + Infos[n].Marking;
                    row.Cells.Add(cell3);


                    //核准
                    TableCell cell4 = new TableCell();

                    cell4.CssClass = "Td";

                    if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "100105"))
                    {
                        if (Infos[n].ApproveScore != -1)
                        {

                            cell4.Text = "<a href=WorkTaskGrade.aspx?T=1&id=" + Infos[n].Id + "&Year=" + YearList.SelectedValue + "&SM=" + StartMonthList.SelectedValue + "&EM=" + EndMonthList.SelectedValue + " >" + Infos[n].ApproveScore + "</a>";
                        }
                        else
                        {
                            cell4.Text = "<a href=WorkTaskGrade.aspx?T=1&id=" + Infos[n].Id + "&Year=" + YearList.SelectedValue + "&SM=" + StartMonthList.SelectedValue + "&EM=" + EndMonthList.SelectedValue + " >评分</a>";
                        }
                    }
                    else
                    {
                        if (Infos[n].ApproveScore != -1)
                        {

                            cell4.Text = Infos[n].ApproveScore.ToString();
                        }
                        else
                        {
                            cell4.Text = "未评分";
                        }
                    }
                    cell4.Wrap = false;
                    row.Cells.Add(cell4);

                    //终审
                    TableCell cell5 = new TableCell();
                    cell5.Wrap = false;
                    cell5.CssClass = "Td";
                    if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "100104"))//终审
                    {

                        if (Infos[n].Score != -1)
                        {

                            cell5.Text = "<a href=WorkTaskGrade.aspx?T=2&id=" + Infos[n].Id + "&Year=" + YearList.SelectedValue + "&SM=" + StartMonthList.SelectedValue + "&EM=" + EndMonthList.SelectedValue + " >" + Infos[n].Score + "</a>";
                            total[j] += Infos[n].Score;
                        }
                        else
                        {
                            cell5.Text = "<a href=WorkTaskGrade.aspx?T=2&id=" + Infos[n].Id + "&Year=" + YearList.SelectedValue + "&SM=" + StartMonthList.SelectedValue + "&EM=" + EndMonthList.SelectedValue + " >评分</a>";
                        }
                    }
                    else
                    {
                        if (Infos[n].Score != -1)
                        {

                            cell5.Text = Infos[n].Score.ToString();
                            total[j] += Infos[n].Score;
                        }
                        else
                        {
                            cell5.Text = "未评分";
                        }
                    }
                    row.Cells.Add(cell5);

                    //加分
                    TableCell cell7 = new TableCell();

                    cell7.CssClass = "Td";
                    if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "100104"))//终审
                    {

                        //ASP.usercontral_popuppanel_ascx cc = new ASP.usercontral_popuppanel_ascx();

                        //cc = this.LoadControl("../../UserContral/PopupPanel.ascx") as ASP.usercontral_popuppanel_ascx;
                        //cc.ID = Guid.NewGuid() + "cc";
                        //cell7.Controls.Add(cc);
                        //cc.SetValue(Infos[n].BonusPoint.ToString(), Infos[n].Id.ToString());
                        //cell7.Text = "<a href=WorkTaskGrade.aspx?T=2&id=" + Infos[n].Id + " >" + Infos[n].BonusPoint + "</a>";

                        cell7.Text = "<a href=WorkTaskGrade.aspx?T=2&id=" + Infos[n].Id + "&Year=" + YearList.SelectedValue + "&SM=" + StartMonthList.SelectedValue + "&EM=" + EndMonthList.SelectedValue + " >" + Infos[n].BonusPoint + "</a>";
                        total[j] += Infos[n].BonusPoint;
                    }
                    else
                    {


                        cell7.Text = Infos[n].BonusPoint.ToString();
                        total[j] += Infos[n].BonusPoint;

                    }
                    row.Cells.Add(cell7);

                    tb.Rows.Add(row);


                    if (ItemCount == 1)
                    {

                        for (int m = 1; m < ItemList.Count; m++)
                        {
                            MonthCount++;
                            departentCount++;
                            ItemCount++;
                            WorkTaskItemInfo item = ItemList[m];
                            row = new TableRow();

                            cell = new TableCell();
                            cell.Text = ItemCount + "、" + item.ItemContent;
                            cell.CssClass = "Td";
                            row.Cells.Add(cell);

                            cell = new TableCell();
                            cell.Text = item.Circs;
                            cell.CssClass = "Td";
                            row.Cells.Add(cell);

                            tb.Rows.Add(row);
                        }
                        cell1.RowSpan = ItemCount;
                        cell2.RowSpan = ItemCount;
                        cell3.RowSpan = ItemCount;
                        cell7.RowSpan = ItemCount;
                        cell5.RowSpan = ItemCount;
                        cell4.RowSpan = ItemCount;
                    }
                }
                DepartmentCell.RowSpan = departentCount;
            }
            MonthCell.RowSpan = MonthCount;
        }

        #region
        //年度
        int YearCount = 0;
        TableCell YearCell = new TableCell();
        YearCell.Text = "年度";
        YearCell.CssClass = "Td";
        for (int j = 0; j < Departments.Count; j++)
        {
            TableCell DepartmentCell = new TableCell();
            int departentCount = 0;
            DepartmentCell = new TableCell();
            DepartmentCell.CssClass = "Td";
            cell.Wrap = true;
            DepartmentCell.Text = Departments[j].ShortName.Substring(5, Departments[j].ShortName.Length - 5);
            DepartmentCell.Width = Unit.Pixel(100);
            DepartmentCell.Wrap = true;
            //处室任务
            WorkTaskGrade Task = new WorkTaskGrade();
            List<WorkTaskGradeInfo> Infos = Task.GetList("  (IsYear = 1) AND (YEAR(TaskDate) =" + YearList.SelectedValue + ") And Departmentid='" + Departments[j].DepartmentID + "' ");
            for (int n = 0; n < Infos.Count; n++)
            {

                TableRow row = new TableRow();

                row.HorizontalAlign = HorizontalAlign.Right;
                if (YearCount == 0)
                {
                    row.Cells.Add(YearCell);
                }
                if (departentCount == 0)
                {
                    row.Cells.Add(DepartmentCell);
                }
                TableCell cell1 = new TableCell();
                cell1.CssClass = "Td";

                cell1.Text = "<a href=WorkTaskView.aspx?id=" + Infos[n].Id + " >" + Infos[n].WorkTask + "</a>";
                row.Cells.Add(cell1);

                List<WorkTaskItemInfo> ItemList = WorkTaskItem.GetList(" taskId='" + Infos[n].Id + "' order by OrderNo,CreateDate ");
                int ItemCount = 0;

                YearCount++;
                departentCount++;
                ItemCount++;
                if (ItemList.Count > 0)
                {
                    WorkTaskItemInfo item = ItemList[0];
                    cell = new TableCell();
                    cell.Text = ItemCount + "、" + item.ItemContent;
                    cell.CssClass = "Td";
                    cell.Width = 400;
                    row.Cells.Add(cell);

                    cell = new TableCell();
                    cell.Text = item.Circs;
                    cell.CssClass = "Td";


                    row.Cells.Add(cell);

                    tb.Rows.Add(row);


                }
                else
                {
                    cell1.ColumnSpan = 2;

                    //简单情况
                    TableCell cell6 = new TableCell();
                    cell6.Width = 100;
                    cell6.CssClass = "Td";
                    cell6.Text = Infos[n].SimpleCircs;

                    row.Cells.Add(cell6);

                }

                TableCell cell2 = new TableCell();
                cell2.CssClass = "Td";
                cell2.Text = Infos[n].Marking.ToString();
                row.Cells.Add(cell2);


                TableCell cell3 = new TableCell();
                cell3.CssClass = "Td";
                cell3.Wrap = false;
                if (Infos[n].Score != -1)
                {
                    total[j] = total[j] + Infos[n].Score;
                    cell3.Text = Infos[n].Score.ToString();
                }
                else
                {
                    cell3.Text = "未评分";
                }
                all[j] = all[j] + Infos[n].Marking;
                row.Cells.Add(cell3);

                //核准
                TableCell cell4 = new TableCell();
                cell4.Wrap = false;
                cell4.CssClass = "Td";
                cell4.Text = Infos[n].Marking.ToString();

                row.Cells.Add(cell4);

                //终审
                TableCell cell5 = new TableCell();
                cell5.Wrap = false;
                cell5.CssClass = "Td";
                cell5.Text = Infos[n].Marking.ToString();

                row.Cells.Add(cell5);

                //加分
                TableCell cell7 = new TableCell();

                cell7.CssClass = "Td";
                if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "100104"))//终审
                {

                    //ASP.usercontral_popuppanel_ascx cc = new ASP.usercontral_popuppanel_ascx();

                    //cc = this.LoadControl("../../UserContral/PopupPanel.ascx") as ASP.usercontral_popuppanel_ascx;
                    //cc.ID = Guid.NewGuid() + "cc";
                    //cell7.Controls.Add(cc);
                    //cc.SetValue(Infos[n].BonusPoint.ToString(), Infos[n].Id.ToString());
                    //cell7.Text = "<a href=WorkTaskGrade.aspx?T=2&id=" + Infos[n].Id + " >" + Infos[n].BonusPoint + "</a>";

                    cell7.Text = "<a href=javaScript:OpenUrl('" + Infos[n].Id + "') >" + Infos[n].BonusPoint + "</a>";
                    total[j] += Infos[n].BonusPoint;
                }
                else
                {


                    cell7.Text = Infos[n].BonusPoint.ToString();
                    total[j] += Infos[n].BonusPoint;

                }
                row.Cells.Add(cell7);

                if (ItemCount == 1)
                {

                    for (int m = 1; m < ItemList.Count; m++)
                    {
                        YearCount++;
                        departentCount++;
                        ItemCount++;

                        WorkTaskItemInfo item = ItemList[m];
                        row = new TableRow();

                        cell = new TableCell();
                        cell.Text = ItemCount + "、" + item.ItemContent;
                        cell.CssClass = "Td";
                        row.Cells.Add(cell);

                        cell = new TableCell();
                        cell.Text = item.Circs;
                        cell.CssClass = "Td";
                        row.Cells.Add(cell);

                        tb.Rows.Add(row);

                      
                    }
                    cell1.RowSpan = ItemCount;
                    cell2.RowSpan = ItemCount;
                    cell3.RowSpan = ItemCount;
                    cell7.RowSpan = ItemCount;
                    cell5.RowSpan = ItemCount;
                    cell4.RowSpan = ItemCount;
                }


                tb.Rows.Add(row);
            }
            DepartmentCell.RowSpan = departentCount;
        }
        YearCell.RowSpan = YearCount;
        #endregion
        BindChart(all, total);
    }

    private void BindChart(decimal[] all, decimal[] total)
    {

        List<ResultInfo> results = new List<ResultInfo>();
        ResultInfo result = null;
        List<DepartmentInfo> Departments = Department.GetList("Left(departmentid,6)='" + Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID.Substring(0, 6) + "'");
        for (int i = 0; i < Departments.Count; i++)
        {

            if (total[i] != 0)
            {
                result = new ResultInfo();
                result.DepartmentName = Departments[i].ShortName.ToString();
                decimal cc = total[i];
                result.Score = Convert.ToDecimal(cc.ToString("0.00"));
                results.Add(result);
            }



        }
        Comparer c = new Comparer();
        results.Sort(c);
        DepartmentInfo department = Department.GetDepartment(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID.Substring(0, 6) + "0");
        UserChart.Titles["Title"].Text = YearList.SelectedItem.Text + department.ShortName + "年度工作量化评分情况统计表";
        UserChart.ChartAreas["Default"].AxisX.LabelsAutoFit = false;
        UserChart.Series["综合得分"].Points.DataBindXY(results, "DepartmentName", results, "Score");
    }

    protected void ButtonEvaluate_Click(object sender, EventArgs e)
    {
        CreateTable();
    }
}
